<!DOCTYPE html>
<html>

<head>
    <title>VectorFieldAnim</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    <link rel="stylesheet" href="//unpkg.com/leaflet@1.2.0/dist/leaflet.css" />
    <link rel="stylesheet" href="examples.css" />
    <link href="https://fonts.googleapis.com/css?family=Roboto:100,400" rel="stylesheet">
</head>

<body>
    <h1 class="title mapTitle">VectorFieldAnim</h1>
    <div id="map"></div>

    <!-- CDN -->
    <script src="//d3js.org/d3.v4.min.js"></script>
    <script src="//npmcdn.com/leaflet@1.2.0/dist/leaflet.js"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/chroma-js/2.1.0/chroma.min.js"></script>

    <!-- Plugin -->
    <script src="dist/leaflet.canvaslayer.field.js"></script>

    <script>
        let map = L.map('map');

        /* Basemap */
        let url = 'https://cartodb-basemaps-{s}.global.ssl.fastly.net/dark_nolabels/{z}/{x}/{y}.png';
        L.tileLayer(url, {
            attribution: 'OSM & Carto',
            subdomains: 'abcd',
            maxZoom: 19
        }).addTo(map);

        /* Default vectorfield animation, from two ASCIIGrid files with u|v current velocity */
        d3.text('data/Bay_U.asc', function (u) {
            d3.text('data/Bay_V.asc', function (v) {
                let vf = L.VectorField.fromASCIIGrids(u, v);
                let layer = L.canvasLayer.vectorFieldAnim(vf).addTo(map);
                map.fitBounds(layer.getBounds());

                layer.on('click', function (e) {
                    if (e.value !== null) {
                        let vector = e.value;
                        let v = vector.magnitude().toFixed(2);
                        let d = vector.directionTo().toFixed(0);
                        let html = (`<span class="popupText">${v} m/s to ${d}&deg</span>`);
                        let popup = L.popup()
                            .setLatLng(e.latlng)
                            .setContent(html)
                            .openOn(map);
                    }
                }); // {onClick: callback} inside 'options' is also supported when using layer contructor
            });
        });
    </script>
</body>

</html>